package com.csu.petstore.service.Impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.csu.petstore.common.CommonResponse;
import com.csu.petstore.mapper.UserMapper;
import com.csu.petstore.pojo.User;
import com.csu.petstore.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    public List<User> getUserByUsername(String username) {
        Map<String,Object> userMap = new HashMap<>();
        userMap.put("username",username);
        List<User> users = userMapper.selectByMap(userMap);
        return users;
    }

    @Override
    public String isRightOfUser(String username, String password) {
        List<User> users = getUserByUsername(username);
        if (users.size() == 0)
            return "用户名错误！";
        User user = users.get(0);
        if (user.getPassword().equals(password))
            return "success";
        else
            return "密码错误！";
    }

    @Override
    public void updateUserByUserName(User user,String username) {
        //删除原有数据
        userMapper.deleteById(username);
        userMapper.insert(user);
    }

    @Override
    public boolean isExistUser(String username) {
        List<User> users = getUserByUsername(username);
        if (users.size() == 0)
            return false;
        else
            return true;
    }

    @Override
    public CommonResponse<String> isExistUser1(String username) {
        if (isExistUser(username)){
            return CommonResponse.createForSuccessMessage("exist");
        }
        else
            return CommonResponse.createForSuccessMessage("Not exist");
    }

    @Override
    public void insertUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public void deleteUser(String username) {
        userMapper.deleteById(username);
    }
}
